<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="message" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            评论
          </div>
          <count-to :start-val="0" :end-val="countData.commentCount" :duration="3000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('category')">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="category" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            分类
          </div>
          <count-to :start-val="0" :end-val="countData.categoryCount" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('article')">
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="article" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            文章
          </div>
          <count-to :start-val="0" :end-val="countData.articleCount" :duration="3200" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('tags')">
        <div class="card-panel-icon-wrapper icon-shopping">
          <svg-icon icon-class="tags" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            标签
          </div>
          <count-to :start-val="0" :end-val="countData.tagCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('link')">
        <div class="card-panel-icon-wrapper icon-link">
          <svg-icon icon-class="link" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            友链
          </div>
          <count-to :start-val="0" :end-val="countData.linksCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('notice')">
        <div class="card-panel-icon-wrapper icon-notice">
          <svg-icon icon-class="notice" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            公告
          </div>
          <count-to :start-val="0" :end-val="countData.noticeCount" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('peoples')">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="peoples" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            用户
          </div>
          <count-to :start-val="0" :end-val="countData.userCount" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('log')">
        <div class="card-panel-icon-wrapper icon-log">
          <svg-icon icon-class="log" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            日志
          </div>
          <count-to :start-val="0" :end-val="countData.logsCount" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import { getToken } from '@/utils/auth'
import axios from 'axios'
export default {
  components: {
    CountTo
  },
  data() {
    return {
      countData: {
        articleCount: 0,
        tagCount: 0,
        commentCount: 0,
        categoryCount: 0,
        linksCount: 0,
        userCount: 1,
        noticeCount: 0,
        logsCount: 0
      }
    }
  },
  created() {
    const baseUrl = process.env.VUE_APP_BASE_API
    const token = getToken()
    const articleCount = () => {
      return axios.get(`${baseUrl}/admin/article/articleCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const tagCount = () => {
      return axios.get(`${baseUrl}/admin/tags/tagCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const commentCount = () => {
      return axios.get(`${baseUrl}/admin/comment/commentCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const categoryCount = () => {
      return axios.get(`${baseUrl}/admin/category/categoryCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const linksCount = () => {
      return axios.get(`${baseUrl}/admin/links/linksCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const noticeCount = () => {
      return axios.get(`${baseUrl}/admin/announcement/announcementCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    const logsCount = () => {
      return axios.get(`${baseUrl}/admin/logs/logsCount`, {
        headers: {
          'Authorization': token
        }
      })
    }
    axios.all([
      articleCount(),
      tagCount(),
      commentCount(),
      categoryCount(),
      linksCount(),
      noticeCount(),
      logsCount()
    ])
      .then(axios.spread((article, tag, comment, category, links, notice, logs) => {
        this.countData.articleCount = article.data.result
        this.countData.tagCount = tag.data.result
        this.countData.commentCount = comment.data.result
        this.countData.categoryCount = category.data.result
        this.countData.linksCount = links.data.result
        this.countData.noticeCount = notice.data.result
        this.countData.logsCount = logs.data.result
      }))
  },
  methods: {
    handleSetLineChartData(type) {
      this.$emit('handleSetLineChartData', type)
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;
  .card-panel-col{
    margin-bottom: 32px;
  }
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);
    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }
      .icon-people {
         background: #40c9c6;
      }
      .icon-message {
        background: #36a3f7;
      }
      .icon-money {
        background: #f4516c;
      }
      .icon-shopping {
        background: #34bfa3
      }
      .icon-notice {
        background: #13ce66;
      }
      .icon-link{
        background: green;
      }
      .icon-log{
        background: red;
      }
    }
    .icon-people {
      color: #40c9c6;
    }
    .icon-message {
      color: #36a3f7;
    }
    .icon-money {
      color: #f4516c;
    }
    .icon-shopping {
      color: #34bfa3
    }
    .icon-notice {
      color: #13ce66;
    }
    .icon-link{
      color: green;
    }
    .icon-log {
      color: red;
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-icon {
      float: left;
      font-size: 48px;
    }
    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}
</style>
